package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Great;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.mapper.GreatMapper;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IGreatService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 点赞的服务方法
 */
@Service
public class GreatServiceImpl implements IGreatService {
    @Autowired
    private GreatMapper greatMapper;

    @Override
    public Integer queryForTotalCount(String greatType, Long greatTypeId) {
        return greatMapper.selectForTotalCount(greatType,greatTypeId);
    }

    @Override
    public Great selectGreatByGreatTypeIdAndUserId(String greatType, Long greatTypeId, Long userId) {

        return greatMapper.selectGreatByGreatTypeIdAndUserId(greatType, greatTypeId, userId);
    }

    @Override
    public void removeGreatByGreatTypeIdAndUserId(String greatType, Long greatTypeId, Long userId) {
        greatMapper.removeGreatByGreatTypeIdAndUserId(greatType, greatTypeId, userId);
    }

    @Override
    public void saveGreatByGreatTypeIdAndUserId(String greatType, Long greatTypeId, Great entity) {
        //设置点赞时间
        entity.setGreatTime(new Date());
        greatMapper.saveGreatByGreatTypeIdAndUserId(greatType, greatTypeId, entity);
    }

    //获取指定用户未阅读的点赞数
    @Override
    public Integer selectSponsorNumber(Long sponsorId) {
        return greatMapper.selectSponsorNumber(sponsorId);
    }

    //获取指定用户未阅读的点赞信息内容
    @Override
    public PageInfo selectByAuthorId(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Great> list = greatMapper.selectByAuthorId(qo);
        int size=list.size();
        if (size>0){
            //获取查询到的未阅读的id,进行阅读后的状态改变操作
            Long[] array=new Long[size];
            for(int i=0;i<size;i++){
                array[i] = list.get(i).getId();
            }
            greatMapper.updateState(array);
        }
        return new PageInfo(list);
    }
}
